import { MessageBox } from 'mint-ui';
let mutations = {
    // 添加购物车
    changeCartListDatas(state, data) {
        // 判断购物车是否为空
        // 判断店铺是否存在
        // 判断商品是否存在
        console.log(state);
        console.log(data);
        // 添加到本地存储--添加到数据库

        let isShop = state.cartListDatas.find(item => {
            return item.ru_id == data.ru_id
        });
        console.log(isShop);
        // 商品是否存在
        let isGoods = state.cartListDatas.find(item => {
            return item.children.find(value => {
                return value.goods_id == data.children[0].goods_id
            })
        });
        console.log(isGoods);
        // 是店铺的
        if (isShop) {
            // 是商品
            if (isGoods) {
                // 如果条件成立,说明商品已在
                // 增加数量即可
                for (let i = 0; i < isGoods.children.length; i++) {
                    if (isGoods.children[i].goods_id == data.children[0].goods_id) {
                        isGoods.children[i].buyNum += data.children[0].buyNum;
                    }
                }
            } else {
                // 如果是该店铺的商品但是不是已在商品,那么添加商品
                isShop.children.push(data.children[0]);
            }
        } else {
            state.cartListDatas.push(data);
        }
        // 本地存储--数据库提交
        localStorage.setItem("carts", JSON.stringify(state.cartListDatas));
    },
    // 购物车的减操作
    minusCartNum(state, data) {
        console.log(state);
        console.log(data);
        for (var i = 0; i < state.cartListDatas.length; i++) {
            if (state.cartListDatas[i].ru_id == data.cartgoodsitem.ru_id) {
                state.cartListDatas[i].children[data.index] = data.cartgoodsitem;
            }
        };
        // 更新本地存储的数据
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
    },
    // 购物车的加操作
    addCartNum(state, data) {
        console.log(state);
        console.log(data);
        for (var i = 0; i < state.cartListDatas.length; i++) {
            if (state.cartListDatas[i].ru_id == data.cartgoodsitem.ru_id) {
                state.cartListDatas[i].children[data.index] = data.cartgoodsitem;
            }
        };
        // 更新本地存储的数据
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
    },
    // 商品选择
    goodsSelect(state, data) {
        // 找到当条商品的信息--设置更新后的数据
        for (var i = 0; i < state.cartListDatas.length; i++) {
            if (state.cartListDatas[i].ru_id == data.cartgoodsitem.ru_id) {
                // 更新商品的信息
                state.cartListDatas[i].children[data.index] = data.cartgoodsitem;
                // 影响的店铺的选择
                let flag = state.cartListDatas[i].children.every(item => {
                    return item.isSelect == true
                });
                if (flag) {
                    state.cartListDatas[i].isShopSelect = true;
                } else {
                    state.cartListDatas[i].isShopSelect = false;
                };

                // 全选的影响
                state.isSelectAll = state.cartListDatas.every(item => {
                    return item.isShopSelect == true
                });
                localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
                localStorage.setItem('all', state.isSelectAll);
            };

        };
    },
    // 店铺的选择
    shopSelect(state, data) {
        console.log(data);
        // 直接给state更新当条选择店铺信息
        state.cartListDatas[data.index].isShopSelect = data.cartListData.isShopSelect;
        // 店铺下的商品
        state.cartListDatas[data.index].children.map(item => {
            item.isSelect = state.cartListDatas[data.index].isShopSelect;
        });
        // 全选的影响
        state.isSelectAll = state.cartListDatas.every(item => {
            return item.isShopSelect == true
        });
        // 更新本地存储数据
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
        localStorage.setItem('all', state.isSelectAll);
    },
    selectAll(state) {
        // 1.让自身取反
        state.isSelectAll = !state.isSelectAll;
        // 2.店铺和商品影响
        state.cartListDatas.map((item) => {
            item.isShopSelect = state.isSelectAll;
            item.children.map(value => {
                value.isSelect = state.isSelectAll;
            });
        });
        // 3.更新本地存储数据
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
        localStorage.setItem('all', state.isSelectAll);
    },
    deleteCart(state, data) {
        MessageBox.confirm("您确定要删除吗?").then(() => {
            console.log('确定');
            // 1.删除自身
            for (let i = 0; i < state.cartListDatas.length; i++) {
                // 删除数据
                if (state.cartListDatas[i].ru_id == data.cartgoodsitem.ru_id) {
                    state.cartListDatas[i].children.splice(data.index, 1);
                };
                // 该店铺下的商品为0,该店铺所有的信息删除
                if (state.cartListDatas[i].children.length == 0) {
                    state.cartListDatas.splice(i, 1);
                };
            };
            localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
            // 2.店铺的影响
            for (let i = 0; i < state.cartListDatas.length; i++) {
                if (state.cartListDatas[i].ru_id == data.cartgoodsitem.ru_id) {
                    let rel = state.cartListDatas[i].children.every(item => {
                        return item.isSelect == true
                    });
                    if (rel) {
                        state.cartListDatas[i].isShopSelect = true;
                    }
                };
            };
            localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
            // 3.全选的影响
            // 全选的影响
            state.isSelectAll = state.cartListDatas.every(item => {
                return item.isShopSelect == true
            });
            // 更新本地存储数据
            localStorage.setItem('all', state.isSelectAll);

        }, () => {
            console.log('取消');
        });
    }

};
export default mutations;